﻿using AutoMapper;

using Admin_jiaju.Core;
using Admin_jiaju.Core.Models;
using Admin_jiaju.mvc.Areas.Admin.Controllers;
using Admin_jiaju.mvc.Controllers;
using Admin_jiaju.mvc.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Admin_jiaju.Core.Repositories;
using Admin_jiaju.Application.Students.Dtos;
using Microsoft.EntityFrameworkCore;
using Admin_jiaju.Application.Dtos;
using Admin_jiaju.mvc.Models.Students;

namespace Admin_jiaju.mvc.Areas.Admin.Controllers
{
    [Area("admin")]
    public class DesignerController : BaseController
    {

        private readonly ILogger<HomeController> _logger;

        public IRepository<Designer, int> _repository { get; }
      
        private readonly IMapper _mapper;
        public DesignerController(
            ILogger<HomeController> logger,
            IRepository<Designer, int> repository,

            IMapper mapper

            )
        {
            _logger = logger;
            _repository = repository;
            _mapper = mapper;

        }
        [HttpGet]
        public async Task<IActionResult> List(StudnetInputDto studnetInputDto)
        {

            var query = _repository.GetAll();
            if (!string.IsNullOrEmpty(studnetInputDto.Name))
            {
                query = query.Where(s => s.Name.Contains(studnetInputDto.Name));
            }
            var count = query.Count();

            query = query.OrderBy(t=>t.Id).Skip((studnetInputDto.Page - 1) * studnetInputDto.Limit).Take(studnetInputDto.Limit);
            //var stus =await query.Include(s=>s.StuClass).AsNoTracking().ToListAsync();//AsNoTracking不对实体类进行追踪
            var stus = await query.AsNoTracking().ToListAsync();
            //var dtos = new PagedResultDto<Designer>
            //{
            //    TotalCount = count,
            //    Page = studnetInputDto.Page,
            //    Limit = studnetInputDto.Limit,
            //    Data = stus,
            //    FilterText = studnetInputDto.FilterText,
            //    Sorting = studnetInputDto.Sorting
            //};

            //AsNoTracking不对实体类进行追踪
            //_mapper.Map < List < NewsView >>
            //return Json(ListResult(_mapper.Map<List<DesignerViewModel>>(stus.Data), stus.TotalCount));
            return Json(ListResult(_mapper.Map<List<DesignerViewModel>>(stus), count));
            // return Json(ListResult(stuts, count));
        }
        public IActionResult Index()
        {
            return View();
        }
        [HttpGet]
        public IActionResult Add()
        {
            return View();
        }
        [HttpPost]
        public async Task<IActionResult> Add(DesignerViewModel ViewModel)
        {
            await _repository.InsertAsync(_mapper.Map<Designer>(ViewModel));
            bool a = await _repository.SavesAsync();
            if (a == true)
            {
                return Json(new { success = true, msg = "新增成功" });
            }
            else
            {
                return Json(new { success = false, msg = "新增失败" });
            }
        }
        public async Task<IActionResult> Edit(int id)
        {
            var pro = await _repository.FirstOrDefaultAsync(s => s.Id == id);
            return View(pro);
        }
        [HttpPost]
        public async Task<ActionResult> Edit(int id, Designer list)
        {
            if (id != list.Id)
            {
                return BadRequest();
            }
            await _repository.UpdateAsync(list);
            return Json(AjaxResult(true));
        }
        public async Task<ActionResult> Delete(int id, Designer data)
        {
            if (id != data.Id)
            {
                return Json(AjaxResult(false));
            }
            await _repository.DeleteAsync(data);
            return Json(AjaxResult(true));
        }
       
    }
}
